package com.dyypdadmin.controller;

import com.dyypdadmin.pojo.Pets;
import com.dyypdadmin.result.Result;
import com.dyypdadmin.service.PetsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pets")
public class PetsController {

    @Autowired
    private PetsService petsService;

    /**
     * 根据用户ID获取宠物列表
     * @param userId 用户ID
     * @return 返回包含宠物列表的结果对象
     */
    @GetMapping("/getbyuserid/{user_id}")
    public Result getPetsByUserId(@PathVariable("user_id") Integer userId) {
        List<Pets> petsList = petsService.getPetsByUserId(userId);
        if (petsList != null) {
            return new Result(true, petsList);
        } else {
            return new Result(true, "请求成功，数据库中没有宠物信息");
        }
    }

    /**
     * 添加宠物
     * @param pet 宠物对象
     * @return 返回操作结果的结果对象
     */
    @PostMapping("/add")
    public Result addPet(@RequestBody Pets pet) {
        String pet_name = pet.getPet_name();
        Integer user_id = pet.getUser_id();
        String breed = pet.getBreed();
        String description = pet.getDescription();
        int result = petsService.addPet(user_id, pet_name, breed, description);
        if (result > 0) {
            return new Result(true, "添加成功");
        } else {
            return new Result(false, "添加失败");
        }
    }

    /**
     * 更新宠物信息
     * @param pet 宠物对象
     * @return 返回操作结果的结果对象
     */
    @PutMapping("/update")
    public Result updatePet(@RequestBody Pets pet) {
        Integer pet_id = pet.getPet_id();
        String pet_name = pet.getPet_name();
        String breed = pet.getBreed();
        String description = pet.getDescription();
        int result = petsService.updatePet(pet_id, pet_name, breed, description);
        if (result > 0) {
            return new Result(true, "修改成功");
        } else {
            return new Result(false, "修改失败");
        }
    }

    /**
     * 根据宠物ID删除宠物
     * @param pet_id 宠物ID
     * @return 返回操作结果的结果对象
     */
    @GetMapping("/delete/{pet_id}")
    public Result deletePetById(@PathVariable("pet_id") Integer pet_id) {
        int result = petsService.deletePetById(pet_id);
        if (result > 0) {
            return new Result(true, "删除成功");
        } else {
            return new Result(false, "删除失败");
        }
    }
    /**
     * @description:
     * @author:
     * @date: 2024/3/8 下午 09:07
     * @param: 
     * @return: 
     **/
    @GetMapping("/getPetCounts")
    public Result getPetCounts(){
        int result = petsService.getPetCounts();
        return new Result(true,result);

    }
}
